package config

import (
	"sync"
	"time"
)

var Conf Config

type Config struct {
	Mode   string
	Notify NotifyConfig
	Docker DockerConfig
}
type NotifyConfig struct {
	Active bool
	Url    string
	Method string
	Data   map[string]string
}
type DockerConfig struct {
	Restart   bool
	Container string
	Retry     int
}

var Counter = GlobalCounter{
	Count: 0,
	Time:  time.Now(),
}

type GlobalCounter struct {
	Count int
	Time  time.Time
	mu    sync.Mutex
}

func (gc *GlobalCounter) AllowExecution(maxExecutions int) bool {
	gc.mu.Lock()
	defer gc.mu.Unlock()
	const timeWindow = 1 * time.Minute
	if time.Since(gc.Time) >= timeWindow {
		gc.Count = 0
		gc.Time = time.Now()
	}
	if gc.Count < maxExecutions {
		gc.Count++
		return true
	}
	return false
}
